package com.demo.my.Controller;

import com.demo.my.Entity.Department;
import com.demo.my.Service.DepartmentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/DepPageServlet")
public class DepPageServlet extends HttpServlet {
    private DepartmentService departmentService=new DepartmentService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String pageNo = req.getParameter("pageNo");
            if (pageNo==null){
                pageNo="1";
            }
            List<Department> list = departmentService.getDepList(pageNo);
            req.setAttribute("list",list);
            int count = departmentService.getDepListCount();
            req.setAttribute("count",count);
            int totalPage = (int) Math.ceil(count / 8.0);
            req.setAttribute("totalPage",totalPage);
            req.setAttribute("pageNo",Integer.parseInt(pageNo));
            req.getRequestDispatcher("jsp/admin/department_list.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
